//
//  FR_EnterAndLeaveMessageController.m
//  FaceRecognition
//
//  Created by zz on 17/7/31.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_EnterAndLeaveMessageController.h"
#import "SY_Activity_Model.h"
#import "SYMessageTool.h"
@interface FR_EnterAndLeaveMessageController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;


@property (nonatomic, strong) UIView *tableHeaderView;

@property (nonatomic, strong) UIImageView *borderImgView; //框
@property (nonatomic, strong) UIImageView *headerImgView; //头像
@property (nonatomic, strong) UIImageView *stateImgView; //进 或者 出

@property (nonatomic, strong) NSDictionary *infoDic;

@end

@implementation FR_EnterAndLeaveMessageController

#pragma mark -Getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, VIEW_WIDTH, VIEW_HEIGHT - 64 ) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.rowHeight = 40;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = self.tableHeaderView;
    }
    
    return _tableView;
}

- (UIView *)tableHeaderView {
    
    if (_tableHeaderView == nil) {
        
        _tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 334)];
    
        //顶部背景图

        UIImageView *topBgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 290)];
        topBgImgView.image = [UIImage imageNamed:@"message_bg"];
        
        [_tableHeaderView addSubview:topBgImgView];
        
        //相框
        _borderImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 206, 206)];
        _borderImgView.center = CGPointMake(SCREENWIDTH / 2, 145);
        _borderImgView.clipsToBounds = NO;
        _borderImgView.image = [UIImage imageNamed:@"message_camera_border"];
        [_tableHeaderView addSubview:_borderImgView];
        
        //头像
        _headerImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 140, 140)];
        _headerImgView.clipsToBounds = YES;
        _headerImgView.layer.cornerRadius = 70;
        _headerImgView.center = _borderImgView.center;
        [self.headerImgView sd_setImageWithURL:[NSURL URLWithString:[self.infoDic objectForKey:@"photo"]]];
        [_tableHeaderView addSubview:_headerImgView];
        
        UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 290, VIEW_WIDTH, 44)];
        bottomView.backgroundColor = [UIColor colorWithHexString:@"#045aa5"];
        [_tableHeaderView addSubview:bottomView];
        
        _stateImgView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 66, 66)];
        if ([[self.infoDic objectForKey:@"inorout"] isEqualToString:@"入"]) {
            _stateImgView.image = [UIImage imageNamed:@"message_enter"];
        }else {
            _stateImgView.image = [UIImage imageNamed:@"message_out"];
        }
        
        _stateImgView.center = CGPointMake(SCREENWIDTH / 2, 290);
        [_tableHeaderView addSubview:_stateImgView];
        
    }
    
    return _tableHeaderView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBar.nav_titleLabel.text = @"消息";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#045aa5"];
    
    [self requestData];
   
   [self changeReadState];
}

- (void)changeReadState{
   
   SY_Activity_Model * model = [[SY_Activity_Model alloc]init];
   model.readState = 1;
   model.infoid = _infoID;
   model.description2 = _description2;
   [SYMessageTool updataMessageModel2:model];
   
}
#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellid = @"cellid";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    
    UILabel *leftLabel,*rightLabel;
    
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        
        leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(38+15, 0, 150, 40)];
        leftLabel.textColor = [UIColor whiteColor];
        leftLabel.font = [UIFont systemFontOfSize:19];
        [cell.contentView addSubview:leftLabel];
        
        rightLabel = [[UILabel alloc] init];
        rightLabel.frame = CGRectMake(SCREENWIDTH - 38 - 15 - 190, 0, 190, 40);
        rightLabel.font = [UIFont systemFontOfSize:19];
        rightLabel.textAlignment = NSTextAlignmentRight;
        rightLabel.textColor = [UIColor whiteColor];
        [cell.contentView addSubview:rightLabel];
        
        
        UIView *linev = [[UIView alloc] initWithFrame:CGRectMake(38, 37.5, SCREENWIDTH - 38 * 2, 0.5)];
        linev.backgroundColor = [UIColor whiteColor];
        [cell.contentView addSubview:linev];
        
        cell.contentView.backgroundColor = [UIColor clearColor];
        cell.backgroundColor = [UIColor clearColor];
    }
    
    switch (indexPath.row) {
        case 0:
            leftLabel.text = @"姓名";
            rightLabel.text = [self.infoDic objectForKey:@"username"];
            break;
        case 1:
            leftLabel.text = @"学校";
            rightLabel.text = [self.infoDic objectForKey:@"scname"];
            break;
        case 2:
            leftLabel.text = @"班级";
            rightLabel.text = [self.infoDic objectForKey:@"classname"];
            break;
        case 3:
            leftLabel.text = @"时间";
            rightLabel.text = [self.infoDic objectForKey:@"zptime"];
            break;
            
        default:
            break;
    }

    return cell;
}

#pragma mark -Request 
- (void)requestData {
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    [paramDic setObject:_infoID forKey:@"infoid"];
    if (self.classID) {
        [paramDic setObject:self.classID forKey:@"classid"];
    }
    [paramDic setObject:[SYAccountTool ShareAccount].account.scid forKey:@"scid"];
    [paramDic setObject:USERID forKey:@"userid"];
    
    [Utils Java_get:@"api/getStuEventParDetail" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dic = [jsonObject objectForKey:@"re_data"];
        
        self.infoDic = [[NSDictionary alloc]initWithDictionary:dic];
        
        //[self.tableView reloadData];
        [self.view addSubview:self.tableView];
        
    } failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
